package com.gzhryc.shared_device.bxscn.core.protocol.v1.msg;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.secruity.CRC16;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * 设备归还结果上报
 * @author zhanghao
 *
 */
public class RSRequest implements Serializable{

	private static final long serialVersionUID = 1L;

	public final static String CMD = "RS";
	
	private String cmd = CMD;
	private String messageId;
	private String deviceSn;
	private Integer resultCode;
	private Integer slotNum;
	private String mobilePowerId = "";				//移动电源 ID
	private Double mobilePowerSupplyVolume;			//移动电源电量
	private String mobilePowerLockStatus = "";		//移动电源锁状态
	private String mobilePowerWireStatus = "";		//移动电源线状态

	public RSRequest() {}
	
	public RSRequest(BaseMessage baseMessage) {
		String[] data = baseMessage.getData();
		this.deviceSn = baseMessage.getDeviceSn();
		this.messageId = baseMessage.getMessageId();

		if(data != null) {
			if(data.length >= 1) {
				this.resultCode = Integer.valueOf(data[0]);
			}
			if(data.length >= 2) {
				this.slotNum = Integer.valueOf(data[1]);
			}
			if(data.length >= 3) {
				this.mobilePowerId = data[2];
			}
			if(data.length >= 4) {
				String temp = data[3];
				if(StringUtils.isNotBlank(temp) && StringTools.isNumber(temp)) {
					this.mobilePowerSupplyVolume = Double.valueOf(temp);
				}
			}
			if(data.length >= 5) {
				this.mobilePowerLockStatus = data[4];
			}
			if(data.length >= 6) {
				this.mobilePowerWireStatus = data[5];
			}
		}
	}

	public String getCmd() {
		return cmd;
	}

	public void setCmd(String cmd) {
		this.cmd = cmd;
	}

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public String getDeviceSn() {
		return deviceSn;
	}

	public void setDeviceSn(String deviceSn) {
		this.deviceSn = deviceSn;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public Integer getSlotNum() {
		return slotNum;
	}

	public void setSlotNum(Integer slotNum) {
		this.slotNum = slotNum;
	}

	public String getMobilePowerId() {
		return mobilePowerId;
	}

	public void setMobilePowerId(String mobilePowerId) {
		this.mobilePowerId = mobilePowerId;
	}

	public Double getMobilePowerSupplyVolume() {
		return mobilePowerSupplyVolume;
	}

	public void setMobilePowerSupplyVolume(Double mobilePowerSupplyVolume) {
		this.mobilePowerSupplyVolume = mobilePowerSupplyVolume;
	}

	public String getMobilePowerLockStatus() {
		return mobilePowerLockStatus;
	}

	public void setMobilePowerLockStatus(String mobilePowerLockStatus) {
		this.mobilePowerLockStatus = mobilePowerLockStatus;
	}

	public String getMobilePowerWireStatus() {
		return mobilePowerWireStatus;
	}

	public void setMobilePowerWireStatus(String mobilePowerWireStatus) {
		this.mobilePowerWireStatus = mobilePowerWireStatus;
	}
	
	public String getData() {
		String data = cmd+","+messageId+","+deviceSn+","+resultCode+","+slotNum+","+mobilePowerId+","+mobilePowerSupplyVolume+","+mobilePowerLockStatus+","+mobilePowerWireStatus;
		return data;
	}
	
	public String getMessage() {
		String data = getData();
		String crc16 = CRC16.get((data+","+ BaseMessage.MESSAGE_KEY).getBytes());
		return "{"+data+","+crc16+"}";
	}
}
